package com.flow.demo.module.gd.service.activities;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.flow.demo.module.gd.controller.admin.activities.vo.*;
import com.flow.demo.module.gd.dal.dataobject.activities.ActivitiesDO;
import com.flow.demo.framework.common.pojo.PageResult;
import com.flow.demo.framework.common.util.object.BeanUtils;

import com.flow.demo.module.gd.dal.mysql.activities.ActivitiesMapper;

import javax.annotation.Resource;

import static com.flow.demo.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.flow.demo.module.gd.enums.ErrorCodeConstants.*;

/**
 * 活动 Service 实现类
 *
 * @author Pompey_hpy
 */
@Service
@Validated
public class ActivitiesServiceImpl implements ActivitiesService {

    @Resource
    private ActivitiesMapper activitiesMapper;

    @Override
    public Integer createActivities(ActivitiesSaveReqVO createReqVO) {
        // 插入
        ActivitiesDO activities = BeanUtils.toBean(createReqVO, ActivitiesDO.class);
        activitiesMapper.insert(activities);
        // 返回
        return activities.getId();
    }

    @Override
    public void updateActivities(ActivitiesSaveReqVO updateReqVO) {
        // 校验存在
        validateActivitiesExists(updateReqVO.getId());
        // 更新
        ActivitiesDO updateObj = BeanUtils.toBean(updateReqVO, ActivitiesDO.class);
        activitiesMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivities(Integer id) {
        // 校验存在
        validateActivitiesExists(id);
        // 删除
        activitiesMapper.deleteById(id);
    }

    private void validateActivitiesExists(Integer id) {
        if (activitiesMapper.selectById(id) == null) {
            throw exception(ACTIVITIES_NOT_EXISTS);
        }
    }

    @Override
    public ActivitiesDO getActivities(Integer id) {
        return activitiesMapper.selectById(id);
    }

    @Override
    public PageResult<ActivitiesDO> getActivitiesPage(ActivitiesPageReqVO pageReqVO) {
        return activitiesMapper.selectPage(pageReqVO);
    }

}